<?php
require './include/global.php';
require './include/validate.php';
require './include/all_left.php';
accessblock(array("Trial","Standard","Pending"));

//Not A Recuiter
if($_SESSION[usertype] != "recruiter" ){
   echo "<meta http-equiv=\"refresh\" content=\"0;URL=errorall.php?errid=rc_1011\">";
   exit;	
}

$status = $_SESSION['status'];
$isTr = 'Trial' != $status && 'Standard' != $status;
$smarty->assign('isTr',$isTr);
$smarty->compile_check = true;

$smarty->display('header.tpl');
////

$smarty->assign('addEdit',"Add");



if($_GET['action']=="editgroup"){
	  $editId = $_POST['editId']+0;
	  if($editId > 0){
			$extraSql = " AND `cgp_id`!='".$editId."' ";
	  }
	  $rs = $DB->fetch_one_array("SELECT * FROM pa_contactgroup WHERE `cgp_title`='".mysql_real_escape_string($_POST['cgp_title'])."' AND `cgp_rcid`= '".$_SESSION['uid']."' ".$extraSql );

	  $err = '';
      if($_POST[cgp_title]==''){
	      $err .= "Group Title is required<br>";
	  }
	  if(!empty($rs)){
		  $err .= "You already have this Group Title, please try a different one.";
	  }
	  if($err == ''){
			if($editId > 0){
				$sql = "UPDATE pa_contactgroup SET cgp_title='".mysql_real_escape_string($_POST['cgp_title'])."', cgp_description='".mysql_real_escape_string($_POST['cgp_description'])."' WHERE cgp_rcid = '".$_SESSION['uid']."' AND cgp_id='".$editId."'";
				$err  = "<h3 style=\"color:red\">Edited Successfully!</h3><meta http-equiv=\"refresh\" content=\"2;URL=groups.php?editId=".$editId."\">";

			}else{
				$sql = "INSERT INTO pa_contactgroup (cgp_rcid, cgp_title, cgp_description, cgp_created ) VALUES ('".$_SESSION['uid']."',
				 '".mysql_real_escape_string($_POST['cgp_title'])."',
				 '".mysql_real_escape_string($_POST['cgp_description'])."',
				 '".time()."'
				)";
				$err  = "<h3 style=\"color:red\">Added Successfully!</h3><meta http-equiv=\"refresh\" content=\"2;URL=groups.php\">";
			}
			$DB->query($sql);
	  }
	  $_GET['editId'] = $editId;
}



if($_GET['editId'] > 0 ){
	if($_GET['ac'] == 'delContact'){
		$err = '';
		  if(empty($_POST[delId])){
			  $err .= "Please select a contact<br>";
		  }
		
		if($err == ''){
			 $delIds = @implode(",",$_POST[delId]);
			 $sql = "DELETE FROM pa_mycontacts WHERE `myc_rcid` = '".$_SESSION['uid']."' AND `myc_id`IN (".$delIds.")";
			 $DB->query($sql);
		}
	}

	$_GetEditId = $_GET['editId'] + 0;
	//getvalues
	if($_GET['country'] == ''){
		$_GET['country'] = 211;
	}

	$smarty->assign('sr',$_GET);

	$rs = $DB->fetch_one_array("SELECT * FROM pa_contactgroup WHERE `cgp_id` = '".$_GetEditId."' AND  `cgp_rcid` = '".$_SESSION['uid']."' ");
	if(empty($rs)){
		echo "<meta http-equiv=\"refresh\" content=\"0;URL=errorall.php?errid=rc_1012\">";
		exit;
	}
	$smarty->assign("s",$rs);
	$smarty->assign('addEdit',"Edit");

	//jobupdate
	$smarty->assign('jobupdate_names',$UPDATE);
	$smarty->assign('state',$state);
	$smarty->assign('country',$country);
	$smarty->assign('industry',$jobcategory);
	$smarty->assign('jobfunction',$jobfunction);
	$rec_mycontacts = array('-- SELECT ALL --','First Name','Last Name','Email','Group','Create Date');
	$smarty->assign('rec_mycontacts',$rec_mycontacts);
	
	$groupOptions[] = '-- SELECT ALL --';
	if(!empty($groups)){
		foreach($groups as $group){
			$groupOptions[$group['cgp_id']] = $group['cgp_title'];
		}
	}
	$smarty->assign('groups',$groupOptions);

	$order = " ORDER BY myc_created  DESC";
	$and_sql = "";

	$addwhere = " AND myc_rcid  = '".$_SESSION[uid]."' ";
	$where = "  WHERE FIND_IN_SET('".$_GetEditId."', `myc_gid`) ".$addwhere.$and_sql;

	if($_GET[action]=="search"){ 
		$arr = array();
		if($_GET[name]!=''){ $arr[] = " (`myc_firstname` like '%".mysql_real_escape_string($_GET[name])."%' OR  `myc_lastname` like '%".mysql_real_escape_string($_GET[name])."%') "; }
		if($_GET[city]!='') { $arr[] = " `myc_city` like  '%".mysql_real_escape_string($_GET[city])."%'  ";}
		if($_GET[state]!='0'&&$_GET[state]!='') { $arr[] = " `myc_state` = '".$_GET[state]."'  ";}
		if($_GET[country]!='0'&&$_GET[country]!='') { $arr[] = " `myc_country` = '".$_GET[country]."'  "; }
		if($_GET[industry]!='0'&&$_GET[industry]!='') { $arr[] = " `myc_industry` = '".$_GET[industry]."'  ";}
		if($_GET[speciality]!='0'&&$_GET[speciality]!='') { $arr[] = " (`myc_speciality1` = '".$_GET[speciality]."' OR `myc_speciality2` = '".$_GET[speciality]."' OR `myc_speciality3` = '".$_GET[speciality]."' ) ";}

		if($_GET[orderby]=='0'||empty($_GET[orderby])){ $by = "myc_id";}
		else{
			$ord   = array('','myc_firstname','myc_lastname','myc_email','myc_gid','myc_created');
			$by = $ord[$_GET[orderby]];
			$get[] = "orderby=$_GET[orderby]";
		}


		//print_r($arr);
		if(!empty($arr)){
			$implode  = @implode("AND", $arr);
			$implode  = " AND ".$implode;
			$where    .= $implode;
		}


		$order = " ORDER BY $by DESC";
	}


	$nav = new buildNav;
	$total = $DB->fetch_one_array("SELECT COUNT(*) AS count FROM pa_mycontacts $where ");
	$nav->total_result = $total['count'];
	$nav->execute("SELECT * FROM pa_mycontacts $where $order");

	$i = 0;
	while($list2 = $DB->fetch_array($nav->sql_result)){
	   $_contacts[$i] = $list2;
	   $i++;
	}

	$page = $nav->pagenav();
	$smarty->assign("contacts",$_contacts);

	if($err!=''){
		$smarty->assign("err2",$err);
	}
	$smarty->assign("page",$page);
	$smarty->display('allleft.tpl');
	$smarty->display('editgroups.tpl');
	$smarty->display('footer.tpl');

	exit;

}


if($_GET[action]=='delGroup'){
	$err = '';
	  if(empty($_POST[delId])){
		  $err .= "Please select a group<br>";
	  }
	
	if($err == ''){
		 $delIds = @implode(",",$_POST[delId]);
		 $sql = "DELETE FROM pa_contactgroup WHERE `cgp_rcid` = '".$_SESSION['uid']."' AND `cgp_id`IN (".$delIds.")";
		 $DB->query($sql);
			
		 
		 foreach($_POST[delId] as $gid){
			 $sql = "SELECT * FROM pa_mycontacts WHERE FIND_IN_SET('".$gid."', `myc_gid`) AND myc_rcid = '".$_SESSION['uid']."'";
			 $_needWashedArr = $DB->fetch_all($sql);
			 if(!empty($_needWashedArr)){
				foreach($_needWashedArr as $_needWash){
					$unwashedIds = @explode(",",$_needWash['myc_id']);
					$k = array_search( $gid ,$unwashedIds); 
					unset($unwashedIds[$k]);
					$washedIds = @implode(",",$unwashedIds);
					$DB->query("UPDATE pa_mycontacts SET myc_gid = '".$washedIds."' WHERE myc_id = '".$_needWash['myc_id']."' AND myc_rcid = '".$_SESSION['uid']."'");
				}
			 }
		 }
	}
}



if($_GET['action'] == 'search'){
	$keywords = trim($_POST['keywords']);
	if($keywords != ''){
		$extraSQL = " AND cgp_title like '%".mysql_real_escape_string($keywords)."%' ";
		$smarty->assign("keywords",$keywords);
	}
}

$nav = new buildNav;
$total = $DB->fetch_one_array("SELECT COUNT(*) AS count FROM pa_contactgroup WHERE cgp_rcid='".$_SESSION['uid']."' $extraSQL");
$nav->total_result = $total['count'];
$nav->execute("SELECT * FROM pa_contactgroup WHERE cgp_rcid='".$_SESSION['uid']."' $extraSQL ORDER BY cgp_created DESC ");

$page = $nav->pagenav();

$i=0;
while($list2 = $DB->fetch_array($nav->sql_result)){
   $datajob[$i]              = $list2;
   $datajob[$i][cgp_created]  = @date("m/d/Y",$list2[cgp_created]);
   $aliasnum     = $DB->fetch_one_array("SELECT COUNT(*) as count FROM pa_mycontacts WHERE FIND_IN_SET('".$datajob[$i][cgp_id]."',`myc_gid`) AND myc_rcid = '".$_SESSION[uid]."' ");
   $datajob[$i][members] = $aliasnum['count'];
   $i++;
}

$smarty->assign("grouplist",$datajob);
$smarty->assign("page",$page);
if($err!=''){
$smarty->assign("err",$err);
}
$smarty->assign("search",$reg_data);
$smarty->display('allleft.tpl');
$smarty->display('groups.tpl');
$smarty->display('footer.tpl');


?>